package com.suke.czx.modules.incentivegrant.controller;

import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.ParamValidateUtil;
import com.suke.czx.common.utils.R;
import com.suke.czx.common.validate.IncentiveGrantGroup;
import com.suke.czx.common.validate.IncentiveGrantUpdateGroup;
import com.suke.czx.modules.incentivegrant.entity.IncentiveGrant;
import com.suke.czx.modules.incentivegrant.service.IncentiveGrantService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 应用模块名称: 绩效发放
 * 代码描述: 绩效发放控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2022/07/22 0:50
 */
@Validated
@RestController
@RequestMapping("/api/incentiveGrant")
@AllArgsConstructor
@Api(value = "IncentiveGrantController", tags = "绩效发放")
public class IncentiveGrantController extends AbstractController {

    private final IncentiveGrantService incentiveGrantService;

    /**
     * 初始化绩效发放
     * @param projectId 项目id
     * @return VO
     */
    @RequestMapping(value = "/initial", method = RequestMethod.POST)
    //@PreAuthorize("hasRole('but:incentiveGrant:initial')")
    public R initial(@NotNull Long projectId, @NotBlank String year, @NotNull Integer version) {
        return incentiveGrantService.initial(projectId, year, version);
    }

    /**
     * 绩效发放
     * @param incentiveGrant 绩效发放实体
     * @return VO
     */
    @RequestMapping(value = "/grant", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:incentiveGrant:grant')")
    public R grant(@Validated(IncentiveGrantGroup.class) IncentiveGrant incentiveGrant, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        return incentiveGrantService.grant(incentiveGrant, getUserId());
    }

    /**
     * 更新绩效发放
     * @param incentiveGrant 绩效发放实体
     * @return VO
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:incentiveGrant:update')")
    public R grant(@Validated(IncentiveGrantUpdateGroup.class) IncentiveGrant incentiveGrant) {
        incentiveGrant.setUpdater(getUserId());
        incentiveGrant.setUpdateTime(new Date());
        return incentiveGrantService.update(incentiveGrant);
    }

    /**
     * 删除绩效发放
     * @param incentiveGrantId 绩效发放id
     * @return VO
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:incentiveGrant:delete')")
    public R delete(@NotNull Long incentiveGrantId) {
        return incentiveGrantService.delete(incentiveGrantId, getUserId());
    }

}